package net.zjitc.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5WithSaltUtil {
    private static final String salt = "ItCast";

    // 将字节数组转换为十六进制字符串
    private static String bytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02x", b));
        }
        return result.toString();
    }
    // 使用指定盐值进行MD5哈希
    public static String hashWithSalt(String data, String salt) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(salt.getBytes());
            byte[] hashedBytes = md.digest(data.getBytes());
            return bytesToHex(hashedBytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}
